package ca.ets.osgi.reservationservice;

import java.util.ArrayList;

import ca.ets.osgi.reservationinterface.IReservationService;

public class ReservationService implements IReservationService {

	//this will contain the list of roomID
	private ArrayList<Object> list;
	
	//
	private ListRoomsInfo lri;
	//
	public ReservationService(){}


	@Override
	public Object makeReservation(String reserver,String roomID,String date, String hourStart,
			String hourEnd) {
		Object obj = null;
		System.out.println("[Trying to make the reservation with these criterion: " + "Reserver: " + reserver + ",RoomID: " + roomID + ",on Date: " + date + ",From: " + hourStart + ",To: " + hourEnd  + "]");
		RoomInfo room = lri.getRoomInfo(reserver, roomID, date, hourStart, hourEnd);
		if(room !=null){
			obj = getListObject(room);
			System.out.println("The reservation was made");
		}
		else {
			System.out.println("Sorry, The reservation was not made");
		}
		
		return obj;
	}


	@Override
	public Object getRoomsWithCharacteristics(ArrayList<Object> alist, String date, String hourStart,
			String hourEnd) {
		Object obj = null;
		lri = ListRoomsInfo.getInstance();
		this.list = alist;
		//lri.setRoomTable(this.list);
		lri.setRoomTable2(this.list);
		ArrayList<Object> olist = getList(lri.getListWithCharacteristics(date, hourStart, hourEnd));
		
		if(olist.isEmpty()){
			//System.out.println("No rooms match your criterions");
		}
		obj = olist;
		
		return obj;
	}
	
	/**
	 * 
	 * @param alist
	 * @return
	 */
	public ArrayList<Object> getList(ArrayList<RoomInfo> alist){
		ArrayList<Object> blist = new ArrayList<Object>();
		//Enumeration<RoomInfo> emu = 
		if(alist !=null){
			for(RoomInfo room :alist){
				ArrayList<Object> clist = new ArrayList<Object>();
				clist.add(room.getRoomID());
				clist.add(room.getDate());
				clist.add(room.getHourStart());
				clist.add(room.getHourEnd());
				blist.add(clist);
			}
		}
				
		return blist;
	}
	
	/**
	 * 
	 * @param room
	 * @return
	 */
	private ArrayList<Object> getListObject(RoomInfo room){
		ArrayList<Object> list = new ArrayList<Object>();
		list.add(room.getRoomID());
		list.add(room.getReserver());
		list.add(room.getDate());
		list.add(room.getHourStart());
		list.add(room.getHourEnd());
		list.add(room.isRoomTaken());
			
		if(list.isEmpty())
			list=null;
		
		return list;
	}
	

}
